Page({
  data: {
    selectedCard: 0,
    showRechargeDialog: false,
    rechargeInfo: {
      amount: 100,
      gift: '10元',
      giftDesc: '10元余额'
    },
    protocolChecked: false,
    balance: 0.00,
    customAmount: '',
    showCustomAmountDialog: false,
    customAmountInput: '',
  },
  onLoad() {
    const balance = wx.getStorageSync('balance') || 0;
    this.setData({ balance });
  },
  onShow() {
    const balance = wx.getStorageSync('balance') || 0;
    this.setData({ balance });
  },
  goBack() {
    wx.switchTab({ url: '/pages/member/index' });
  },
  onSelectCard(e) {
    const idx = Number(e.currentTarget.dataset.idx);
    let rechargeInfo = {};
    if(idx === 0) rechargeInfo = { amount: 100, gift: '10元', giftDesc: '10元余额' };
    if(idx === 1) rechargeInfo = { amount: 500, gift: '80元', giftDesc: '80元余额+500积分' };
    if(idx === 2) rechargeInfo = { amount: 1000, gift: '150元', giftDesc: '150元余额+1000积分' };
    if(idx === 3) {
      this.setData({ showCustomAmountDialog: true, customAmountInput: this.data.customAmount || '' });
      return;
    }
    this.setData({ selectedCard: idx, rechargeInfo });
  },
  onCustomAmountInput(e) {
    this.setData({ customAmountInput: e.detail.value });
  },
  onCustomAmountConfirm() {
    const val = this.data.customAmountInput.trim();
    if (!val) return;
    this.setData({
      customAmount: val,
      showCustomAmountDialog: false,
      selectedCard: 3,
      rechargeInfo: { amount: val, gift: '', giftDesc: '其他金额' }
    });
  },
  onCustomAmountCancel() {
    this.setData({ showCustomAmountDialog: false });
  },
  onShowRechargeDialog() {
    this.setData({ showRechargeDialog: true, protocolChecked: false });
  },
  onInputCustomAmount(e) {
    const val = e.detail.value;
    this.setData({ customAmount: val });
    if (this.data.selectedCard === 3) {
      let rechargeInfo = this.data.rechargeInfo;
      rechargeInfo.amount = val;
      this.setData({ rechargeInfo });
    }
  },
  onRechargeConfirm() {
    if (!this.data.protocolChecked) return;
    this.setData({ showRechargeDialog: false });
    let balance = wx.getStorageSync('balance') || 0;
    let amount = 0;
    if (this.data.selectedCard === 3) {
      amount = parseFloat(this.data.customAmount) || 0;
    } else {
      amount = parseFloat(this.data.rechargeInfo.amount) || 0;
    }
    balance = parseFloat(balance) + amount;
    wx.setStorageSync('balance', balance);
    this.setData({ balance });
    if (amount > 0) {
      let recordList = wx.getStorageSync('recordList') || [];
      recordList.unshift({ amount: amount.toFixed(2), time: new Date().toLocaleString() });
      wx.setStorageSync('recordList', recordList);
    }
    wx.showToast({ title: '充值成功', icon: 'success' });
    setTimeout(() => {
      wx.switchTab({ url: '/pages/member/index' });
    }, 800);
  },
  onToggleProtocol() {
    this.setData({ protocolChecked: !this.data.protocolChecked });
  },
  goToRecord() {
    wx.navigateTo({ url: '/pages/vipcenter/record' });
  }
});
